#include "Matrix2D.h"

Matrix2D::Matrix2D( int initSize,  int initValue)
{
    //Create the matrix and initilize it.
    size = initSize;

    matrix = new int*[size];
    for (int i = 0; i < size; i++)
        matrix[i] = new int[size];

    // Initialized in a non-effect convolution
    for (int i = 0; i < size; i++)
        for (int j = 0; j < size; j++)
            matrix[i][j] = initValue;
}

Matrix2D::~Matrix2D()
{
    // Destroy the matrix
    for (int i = 0; i < size; i++)
        delete[] matrix[i];

    delete[] matrix;
}

Matrix2D::Matrix2D(const Matrix2D& m)
{
    // Copy the matrix passed by parameter. In this kind of constructor, the receiver is not created yet.

    // Create the matrix and initilize it.
    this->size = m.getSize();

    matrix = new int*[size];
    for (int i = 0; i < size; i++)
        matrix[i] = new int[size];

    // Initialized in a non-effect convolution
    for (int i = 0; i < size; i++)
        for (int j = 0; j < size; j++)
            matrix[i][j] = m.get(i,j);
}


Matrix2D & Matrix2D::operator=(const Matrix2D& m)
{
    // Copy the matrix passed by parameter. In this kind of "constructor", the receiver has been created.

    // So.. first let's destroy the matrix
    for (int i = 0; i < size; i++)
        delete[] matrix[i];

    delete[] matrix;

    // Create it again
    this->size = m.getSize();

    matrix = new int*[size];
    for (int i = 0; i < size; i++)
        matrix[i] = new int[size];

    // Initialized in a non-effect convolution
    for (int i = 0; i < size; i++)
        for (int j = 0; j < size; j++)
            matrix[i][j] = m.get(i,j);

    return *this;
}
